package org.jiuling.request.http.model;


import com.alibaba.fastjson2.JSON;
import org.jiuling.request.exception.RequestRuntimeException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请求内容
 * @author 紫灵
 * @date 2022年04月16日 14时54分55秒
 */
public class HttpRequestBody extends HashMap<String,Object> implements Map<String,Object> {

    /** 请求内容 */
    private String body;

    private HttpRequestBodyType httpRequestBodyType = HttpRequestBodyType.NULL;

    public String getBody() throws RequestRuntimeException {
        switch (httpRequestBodyType){
            case STRING:
                return this.body;
            case MAP:
                return JSON.toJSONString(this);
            case NULL:
                return null;
            default:
                throw new RequestRuntimeException("未知请求内容类型！");
        }
    }

    /**
     * 添加String类型的请求内容
     * @param body 内容
     * @param isSuperaddition 是否追加参数
     * @return 请求内容
     */
    public HttpRequestBody add(String body, boolean isSuperaddition) throws RequestRuntimeException {
        if(!HttpRequestBodyType.STRING.equals(httpRequestBodyType) && !HttpRequestBodyType.NULL.equals(httpRequestBodyType)){
            throw new RequestRuntimeException("请求内容类型为Map类型，无法设置String类型请求内容");
        }
        this.httpRequestBodyType = HttpRequestBodyType.STRING;
        if(isSuperaddition){
            this.body = this.body + body;
        }else {
            this.body = body;
        }
        return this;
    }

    /**
     * 添加String类型的请求内容
     * @param body 内容
     * @return 请求内容
     */
    public HttpRequestBody add(String body) throws RequestRuntimeException {
        return this.add(body,false);
    }

    /**
     * 添加Map类型请求内容
     * @param key 键
     * @param value 值
     * @param isSuperaddition 是否追加参数
     * @return 请求内容
     */
    public HttpRequestBody add(String key, Object value, boolean isSuperaddition) throws RequestRuntimeException {
        if(!HttpRequestBodyType.MAP.equals(httpRequestBodyType) && !HttpRequestBodyType.NULL.equals(httpRequestBodyType)){
            throw new RequestRuntimeException("请求内容类型为String类型，无法设置Map类型请求内容");
        }
        this.httpRequestBodyType = HttpRequestBodyType.MAP;
        if(isSuperaddition){
            if(!super.containsKey(key)){
                super.put(key,value);
            }else {
                Object oldValue = super.get(key);
                // 判断类型是否为List
                if (oldValue instanceof List) {
                    ((List<Object>) oldValue).add(value);
                } else {
                    List<Object> newValue = new ArrayList<>();
                    newValue.add(oldValue);
                    newValue.add(value);
                    super.put(key, newValue);
                }
            }
        }else {
            super.put(key,value);
        }
       return this;
    }

    /**
     * 添加Map类型请求内容
     * @param key 键
     * @param value 值
     * @return 请求内容
     */
    public HttpRequestBody add(String key, Object value) throws RequestRuntimeException {
        return this.add(key,value,false);
    }

    enum HttpRequestBodyType{
        NULL("null","空类型"),
        STRING("String","字符串类型"),
        MAP("Map","Map集合类类型");

        private final String type;
        private final String msg;

        HttpRequestBodyType(String type, String msg) {
            this.type = type;
            this.msg = msg;
        }

        public String getType() {
            return type;
        }

        public String getMsg() {
            return msg;
        }
    }
}
